package com.ruifu.pls.repository;

import com.ruifu.pls.entity.DocOrderHeader;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import javax.transaction.Transactional;

/**
 * ********************************************************
 * * @desc  ：
 * * @author  ${user}
 * * @date    ${date}
 * * @version v1.0
 * * ${tags}
 * *******************************************************
 */
public interface GDocOrderHeaderRepository extends
        PagingAndSortingRepository<DocOrderHeader,String>, JpaSpecificationExecutor<DocOrderHeader> {
    //出库单确认接口（只查99状态）
    @Query(value="SELECT * from (select * from doc_order_header where (edisendflag = 'N' or edisendflag = 'J') AND (warehouseid='NSLDS' OR warehouseid = 'JD_TEST') AND ordertype not IN ('XSCK','FHCK','HHCK','BFCK') AND SOSTATUS = 99 order by addtime desc) where ROWNUM = 1 ",nativeQuery=true)
    public DocOrderHeader findByDocOrderHeader();
   //订单状态即时更新接口
    @Query(value="SELECT * from (select * from doc_order_header where  (warehouseid='NSLDS' OR warehouseid = 'JD_TEST')  and (USERDEFINE5 is null or USERDEFINE5!='11')  and (edisendflag = 'N' or edisendflag = 'J') order by edittime desc) where ROWNUM = 1 ",nativeQuery=true)
    public DocOrderHeader findByDocOrderHeaderTwo();
    //销售订单确认接口（只查99状态）
    @Query(value="SELECT * from (select * from doc_order_header where edisendflag = 'N' AND (warehouseid='NSLDS' or warehouseid = 'JD_TEST') and (edisendflag = 'N' or edisendflag = 'J')  and ordertype IN ('XSCK','FHCK','HHCK','BFCK') AND SOSTATUS = 99 order by addtime desc) where ROWNUM = 1 ",nativeQuery=true)
    public DocOrderHeader findByDocOrderHeaderForConform();
    //出库单新增接口
    @Query(value="SELECT * from (select * from doc_order_header where edisendflag = 'N' AND warehouseid='NSLDS'  and ordertype!='XSCK' AND SOSTATUS = 00 order by addtime desc) where ROWNUM = 1 ",nativeQuery=true)
    public DocOrderHeader findByDocOrderHeaderForReverseAdd();
    //销售订单新增接口
    @Query(value="SELECT * from (select * from doc_order_header where edisendflag = 'N' AND warehouseid='NSLDS' and ordertype='XSCK' AND SOSTATUS = 00 order by addtime desc) where ROWNUM = 1 ",nativeQuery=true)
    public DocOrderHeader findByDocOrderHeaderAddConform();
    //出库单确认更新状态
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="UPDATE  doc_order_header SET edisendflag = 'Y' WHERE   ORDERNO =?1 ",nativeQuery=true)
    public void findByReturnStatusUpdate(String orderno);
    //出库单确认错误状态更新
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="UPDATE  doc_order_header SET edisendflag = 'E' WHERE   ORDERNO =?1 ",nativeQuery=true)
    public void findByReturnStatusUpdateEorro(String orderno);
    //出库单新增已回传更新标记
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="UPDATE  doc_order_header SET edisendflag = 'J' WHERE   ORDERNO =?1 ",nativeQuery=true)
    public void findByReturnUpdate(String orderno);
    //出库单新增错误更新状态
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="UPDATE  doc_order_header SET edisendflag = 'E' WHERE   ORDERNO =?1 ",nativeQuery=true)
    public void findByReturnUpdateEE(String orderno);

}
